package it.univpm.diiga.memorysim.views;


import java.util.ArrayList;

import it.univpm.diiga.memorysim.data.DataPlugin;

import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.layout.GridData;

public class SegWthPag extends ViewPart {

	public static final String ID = "it.univpm.diiga.memorysim.views.SegWthPag";
	private Composite top = null;
	private Table process = null;
	private TableColumn ColID,ColSeg;
	private Button deleteProcess = null;
	private Button newProcess = null;
	private Button ok = null;
	@Override
	public void createPartControl(Composite arg0) {
		// TODO Auto-generated method stub
		GridData gridData47 = new GridData();
		gridData47.horizontalAlignment = GridData.FILL;
		gridData47.verticalAlignment = GridData.CENTER;
		GridData gridData46 = new GridData();
		gridData46.horizontalAlignment = GridData.FILL;
		gridData46.verticalAlignment = GridData.CENTER;
		GridData gridData20 = new GridData();
		gridData20.horizontalAlignment = GridData.FILL;
		gridData20.verticalAlignment = GridData.CENTER;
		GridData gridData = new GridData();
		gridData.verticalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = false;
		gridData.verticalSpan = 5;
		gridData.grabExcessVerticalSpace = false;
		gridData.horizontalSpan = 2;
		gridData.heightHint = -1;
		gridData.widthHint = 600;
		gridData.horizontalAlignment = GridData.CENTER;
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		gridLayout.verticalSpacing = 18;
		gridLayout.horizontalSpacing = 20;
		top = new Composite(arg0, SWT.NONE);
		top.setLayout(gridLayout);
		process = new Table(top,SWT.FULL_SELECTION | SWT.HIDE_SELECTION|SWT.BORDER);
		final TableEditor editor = new TableEditor(process);
	    editor.horizontalAlignment = SWT.LEFT;
	    editor.grabHorizontal = true;
	    editor.minimumWidth = 50;
		new TableEditor(process); 
		newProcess = new Button(top, SWT.NONE);
		newProcess.setText("Nuovo processo");
		newProcess.setLayoutData(gridData47);
		newProcess.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {	}

			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem item1 = new TableItem (process, SWT.NONE);
				item1.setText (0,process.getItemCount()+"");
				item1.setText (1, "");	
			}
          });
		deleteProcess = new Button(top, SWT.NONE);
		deleteProcess.setText("Elimina processo");
		deleteProcess.setLayoutData(gridData20);
		deleteProcess.addSelectionListener(new SelectionListener(){

			@Override
			public void widgetDefaultSelected(SelectionEvent e) { }

			@Override
			public void widgetSelected(SelectionEvent e) {
				int index=process.getItemCount()-1;
				if(index>=0) process.remove(index);
			}
			
		});
		process.setHeaderVisible(true);
		process.setLayoutData(gridData);
		process.setLinesVisible(true);
		ok = new Button(top, SWT.NONE);
		ok.setText("Applica");
		ok.addSelectionListener(new SelectionListener(){

			@Override
			public void widgetDefaultSelected(SelectionEvent e) { }

			@Override
			public void widgetSelected(SelectionEvent e) {
				try{
					updateData();
					nextView();
					}catch(Exception noNum){
						MessageDialog.openError(null, "Error", "Dati inseriti errati:" + noNum.getMessage());
					}
			}
			
		});
		new TableViewer(process);
		ColID = new TableColumn(process, SWT.CENTER);
		ColID.setText("ID Processo");
		ColID.setWidth(300);
		ColSeg = new TableColumn(process, SWT.CENTER);
		ColSeg.setText("Numero segmenti");
		ColSeg.setWidth(300);
		process.addSelectionListener(new SelectionAdapter() {
		      public void widgetSelected(SelectionEvent e) {
			        // Clean up any previous editor control
			        Control oldEditor = editor.getEditor();
			        if (oldEditor != null)
			          oldEditor.dispose();

			        // Identify the selected row
			        TableItem item = (TableItem) e.item;
			        if (item == null)
			          return;

			        // The control that will be the editor must be a child of the
			        // Table
			        Text newEditor = new Text(process, SWT.NONE);
			        newEditor.setText(item.getText(1));
			        newEditor.addModifyListener(new ModifyListener() {
			          public void modifyText(ModifyEvent me) {
			            Text text = (Text) editor.getEditor();
			            editor.getItem()
			                .setText(1, text.getText());
			          }
			        });
			        newEditor.selectAll();
			        newEditor.setFocus();
			        editor.setEditor(newEditor, item, 1);
			      }
			    });
			  
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}
	
	public void updateData(){
		if(process.getItemCount()<=0) MessageDialog.openWarning(null, "Warning", "Non hai inserito nessun processo");
		DataPlugin.listSegmentP= new ArrayList<Integer>();
		for(int i=0;i<process.getItemCount();i++){
			try{
				
			int n=Integer.parseInt(process.getItem(i).getText(1));
			DataPlugin.listSegmentP.add(n);
			}catch(Exception noNum){
				MessageDialog.openError(null, "Error", "Dati inseriti errati:" + noNum.getMessage());
				break;
			}
		}
	}
	
	public void nextView(){
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		try {
			int n=0;
		      for(int i=0;i<DataPlugin.listProcess.size();i++){
		    	  n+=DataPlugin.getDimProcess(i);
		      }
			 window.getActivePage().showView(SequenceSimulationSeg.ID, "Seq Sim", IWorkbenchPage.VIEW_ACTIVATE);
		} catch (PartInitException e1) {
			MessageDialog.openError(window.getShell(), "Error", "Error opening view:" + e1.getMessage());
		}
	}
}

